<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category UserService.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/3/3  18:13
 * +--------------------------------------------------------+
 */

namespace App\Services;

use App\Repositories\AdminRepository;
use Illuminate\Support\Facades\Hash;

class AdminService extends BaseService
{
    protected $adminRepository;

    public function __construct(AdminRepository $adminRepository)
    {
        $this->adminRepository = $adminRepository;
    }

    public function getValue($id)
    {
        return $user = $this->adminRepository->getValue($id);
    }

    /**
     * @param array $params
     * @return array
     * @Depiction 获取列表
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  17:06
     */
    public function getList(array $params): array
    {
        $list = $this->adminRepository->getList($params);

        $list = $this->initPaginateData($list);

        foreach ($list['data'] as &$l) {
            $l['role_ids']   = is_string($l['role_ids']) ? explode(',', $l['role_ids']) : [];
            $l['role_names'] = is_string($l['role_names']) ? explode(',', $l['role_names']) : [];
        }

        return $list;
    }

    /**
     * @param array $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  17:09
     */
    public function create(array $params)
    {
        $params['password'] = Hash::make($params['password']);

        $create = $this->adminRepository->create($params);

        return $create;

    }

    /**
     * @param array $params
     * @return bool|int
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  17:48
     */
    public function update(array $params)
    {
        if (isset($params['password'])) {
            $params['password'] = Hash::make($params['password']);
        }


        $create = $this->adminRepository->update($params);

        return $create;

    }

}
